import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ClassSerializerInterceptor,
  UseInterceptors,
  UseGuards,
  Req,
  Query,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto, UpdateUserPassWordDto } from './dto/update-user.dto';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { Users } from './entities/user.entity';
import { AuthGuard } from '@nestjs/passport';
import { Roles, RolesGuard } from '../auth/role.guard';
import { QueryUserDto } from './dto/find-user.dto';

@ApiTags('用户')
@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @ApiOperation({ summary: '注册用户' })
  @ApiResponse({ type: [Users] })
  @UseInterceptors(ClassSerializerInterceptor)
  @Post()
  create(@Body() createUserDto: CreateUserDto) {
    return this.usersService.create(createUserDto);
  }

  @ApiOperation({ summary: '返回用户列表' })
  @ApiResponse({ type: [Users] })
  @ApiBearerAuth() // swagger文档设置token
  @UseGuards(AuthGuard('jwt'))
  @Get()
  findAll(@Query() query: QueryUserDto) {
    return this.usersService.findAll(query);
  }

  @ApiOperation({ summary: '获取token用户信息' })
  @ApiResponse({ type: [Users] })
  @ApiBearerAuth() // swagger文档设置token
  @UseGuards(AuthGuard('jwt'))
  @Get('getUserInfo')
  getUserInfo(@Req() req) {
    return req.user;
  }

  @ApiOperation({ summary: '获取指定用户信息' })
  @ApiResponse({ type: [Users] })
  @ApiBearerAuth() // swagger文档设置token
  @UseGuards(AuthGuard('jwt'))
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.usersService.findOne(id);
  }

  @ApiOperation({ summary: '修改个人信息' })
  @ApiBearerAuth()
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  @Patch('/updateMyInfo')
  updateMyInfo(@Req() req, @Body() updateUserDto: UpdateUserDto) {
    return this.usersService.update(req.user.id, updateUserDto);
  }

  @ApiOperation({ summary: '修改人员信息' })
  @ApiBearerAuth()
  @Roles('root')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.usersService.update(id, updateUserDto);
  }

  @ApiOperation({ summary: '密码重置' })
  @ApiBearerAuth()
  @Roles('root')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  @Patch('/resetPassword/:id')
  resetPassword(@Param('id') id: string) {
    return this.usersService.resetPassword(id);
  }

  @ApiOperation({ summary: '修改密码' })
  @ApiBearerAuth()
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  @Patch('/updatePassword/:id')
  updatePassword(
    @Req() req,
    @Body() updateUserPassWordDto: UpdateUserPassWordDto,
  ) {
    return this.usersService.updatePassword(
      req.user.id,
      updateUserPassWordDto.oldPassWord,
      updateUserPassWordDto.newPassWord,
    );
  }

  @ApiOperation({ summary: '删除人员信息' })
  @ApiBearerAuth()
  @Roles('root')
  @UseGuards(AuthGuard('jwt'), RolesGuard)
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.usersService.remove(id);
  }
}
